package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.api.BaseRest;
import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.bean.UserBean;
import com.xieyingwangluo.yxpart.entity.UserInfoModel;
import com.xieyingwangluo.yxpart.entity.UserModel;
import com.xieyingwangluo.yxpart.service.UserInfoService;
import com.xieyingwangluo.yxpart.service.UserService;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.List;

@Component
public class UserInfoLogic extends BaseRest {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    //添加
//    public Result add(HttpSession session, UserInfoModel userInfoModel) {
//        UserModel user = (UserModel) session.getAttribute("user");
//
//        userInfoModel.setNoid(StringBuildUtils.buildUUID());
//        userInfoModel.setUser_id(user.getNoid());//登陆用户id
//        userInfoModel.setSex(0);//男
//        userInfoModel.setState(1);//1未审核
//        int cnt = userInfoService.add(userInfoModel);
//        if (cnt > 0) {
//            return Result.generate(0, "Add success", cnt);
//        }
//        return Result.generate(1, "Add fail", null);
//    }

    //个人评价：
    //显示：用户信息编号（noid)

    //显示用户详情

    public Result selInfo(String noid) {
        if (noid != null || !"".equals(noid)) {
            UserInfoModel userInfoModel = userInfoService.selO(noid);
            UserModel userModel = userService.getById(userInfoModel.getUser_id());
            UserBean userBean = new UserBean();
            userBean.setUserInfoModel(userInfoModel);
            userBean.setNoid(userModel.getNoid());
            userBean.setTel(userModel.getTel());
            userBean.setName(userModel.getName());
            userBean.setHead_img(userModel.getHead_img());
            userBean.setIntg(userModel.getIntg());
            userBean.setState(userModel.getState());
            userBean.setVip(userModel.getVip());
            userBean.setCreate_time(userModel.getCreate_time());

            if (userBean != null) {
                return Result.generate(0, "Select success", userBean);
            }
            return Result.generate(1, "Select fail", null);
        }
        return Result.generate(1, "Select fail", null);
    }

    //显示：登录用户信息编号（noid）
    public Result selO(String noid) {
        UserInfoModel userInfoModel = userInfoService.selO(noid);
        if (userInfoModel != null) {
            return Result.generate(0, "Select success", userInfoModel);
        }
        return Result.generate(1, "Select fail", null);
    }

    //修改状态：用户信息编号(userinfo_id)，状态(state)
    //管理员审核普通用户
    public Result updS(HttpSession session, int state) {
        UserModel user = (UserModel) session.getAttribute("user");

        int cnt = userInfoService.updS(user.getNoid(), state);
        return Result.generate(0, "Upd success", cnt);
    }

    //查询用户信息列表
    public Result selA(UserInfoModel userInfoModel) {
        if (userInfoModel != null) {
            List<UserInfoModel> userInfoModelList = userInfoService.selA(userInfoModel);
            return Result.generate(0, "Select success", userInfoModelList);
        }
        return Result.generate(1, "Select  fail", null);
    }

    //单个用户信息显示
//    public Result selN(String noid) {
//        if (noid != null || !"".equals(noid)) {
//            UserInfoModel userInfoModel = userInfoService.selO(noid);
//            return Result.generate(0, "Select success", userInfoModel);
//        }
//        return Result.generate(1, "Select  fail", null);
//    }

    //更新用户信息
    public Result upd(UserInfoModel userInfoModel) {
        int cnt = userInfoService.upd(userInfoModel);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }
}
